(ns forum.templates
  (:use  korma.core
         sandbar.stateful-session
         forum.models)
  (:require [net.cgrand.enlive-html :as enlive]))

(defn format-date [date-time]
  (let [date-formatter (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss")]
    (.format date-formatter (date-time))))
 
(enlive/deftemplate home-page  "home.html" [categories]
  [:title] (enlive/content "Forum Clojure")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [category categories]
                [:td.categoryname :a.title] (enlive/set-attr :href (str "/" (:id category)))
                [:td.categoryname :a.title] (enlive/content (:title category))
                [:td.categorydescription] (enlive/html-content (:description category))))


(enlive/deftemplate topic-page  "topics.html" [topics]
  [:title] (enlive/content "Forum topics")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [topic topics]
                [:td.topictitle :a.title] (enlive/set-attr :href (str "/post/" (:id topic)))
                [:td.topictitle :a.title] (enlive/content (:title topic))
                [:td.creator] (enlive/html-content (:username (find-user-by-id (:users_id topic))))
                [:td.edit :a.e] (enlive/set-attr :href (str "/editt/" (:id topic)))
                [:td.edit :a.e] (enlive/content (str "Edit " (:title topic)))
                [:td.delete :a.d] (enlive/set-attr :href (str "/deletetopic/" (:id topic)))
                [:td.delete :a.d] (enlive/content (str "Delete " (:title topic)))))

(enlive/deftemplate post-page  "posts.html" [posts username]
  [:title] (enlive/content "Forum topics")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [post posts]
                [:td.author :a.username] (enlive/set-attr :href (str "/profile/" (:users_id post)))
                [:td.author :a.username] (enlive/html-content (:username (find-user-by-id (:users_id post))))
                [:td.content :div.quote] (if(nil? (:quote post))
                                           (enlive/set-attr :style "display:none")
                                           (enlive/content (:quote post)))
                [:td.content :p.post] (enlive/content (:content post))
                [:td.content :p.datetime] (enlive/html-content 
                (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") (:created post)))
                [:td.content :span.del :a.delete] (enlive/set-attr :href (str "/deletepost/" (:id post)))
                [:td.content :span.edit :a.e] (enlive/set-attr :href (str "/editp/" (:id post)))
                [:td.content :span.quote :a.q] (enlive/set-attr :href (str "/quoterender/" (:id post)))
                [:td.content :span.del] (let [current-user (find-user-by-username username)
                                          role (:role current-user)]
                                      (if-not (or (= role "admin") (= role "moderator"))
                                        (enlive/set-attr :style "display:none")
                                        (enlive/remove-attr :style)))
                [:td.content :p.edit] (let [current-user (find-user-by-username username)
                                          role (:role current-user)]
                                      (if-not (or (= role "admin") (= role "moderator"))
                                        (enlive/set-attr :style "display:none")
                                        (enlive/remove-attr :style)))
                ))

(enlive/deftemplate viewusers-page  "viewusers.html" [users]
  [:title] (enlive/content "Forum Clojure")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [user users]
                [:td.username :a.usr] (enlive/set-attr :href (str "/profile/" (:id user)))
                [:td.username :a.usr] (enlive/content (:username user))
                [:td.password] (enlive/content (:password user))
                [:td.email] (enlive/content (:email user))
                [:td.role] (enlive/content (:role user))
                [:td.edit :a.ed] (enlive/set-attr :href (str "/editu/" (:id user)))
                [:td.edit :a.ed] (enlive/content (str "Edit " (:username user)))))

(enlive/deftemplate profile-page  "profile.html" [user]
  [:title] (enlive/content "Forum Clojure")
     [:div.tablestyle :table :tr.row :td.username] (enlive/content (:username user))
     [:div.tablestyle :table :tr.row :td.email] (enlive/content (:email user))
     [:div.tablestyle :table :tr.row :td.role] (enlive/content (:role user))
     [:div.send :a.button] (enlive/set-attr :href (str "/sendmessage/" (:id user))))

(enlive/deftemplate edituser-page  "edituser.html" [user]
  [:title] (enlive/content "Forum Clojure")
     [:div.box_reg :div.box_reg_body :div.form_row :input.usr] (enlive/set-attr :value (:username user))
     [:div.box_reg :div.box_reg_body :div.form_row :input.pass] (enlive/set-attr :value (:password user))
     [:div.box_reg :div.box_reg_body :div.form_row :input.em] (enlive/set-attr :value (:email user)))

(enlive/deftemplate editpost-page  "editpost.html" [post]
  [:title] (enlive/content "Forum Clojure")
     [:div.box_login :div.box_login_sub :div.form_row :textarea.content] (enlive/content (:content post)))

(enlive/deftemplate edittopic-page  "edittopic.html" [topic]
  [:title] (enlive/content "Forum Clojure")
     [:div.box_login :div.box_login_sub :div.form_row :input.title] (enlive/set-attr :value (:title topic)))

(enlive/deftemplate viewnews-page  "viewnews.html" [news]
  [:title] (enlive/content "Forum Clojure")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [newz news]
                [:td.news :div.title :h3] (enlive/content (:title newz))
                [:td.news :div.content :p] (enlive/html-content (:content newz))
                [:td.news :div.author :h4] (enlive/html-content (:username (find-user-by-id (:users_id newz))))))

(enlive/deftemplate reply-page  "reply.html" [post]
  [:title] (enlive/content "Forum topics")
  [:div.box_login :div.box_login_sub :div.quote]  (enlive/html-content (:content post)))

(enlive/deftemplate message-page  "message.html" [message]
  [:title] (enlive/content "Forum topics")
  [:div.tablestyle :table :tr.row :td.sender :a.username] (enlive/set-attr :href (str "/profile/" (:sender_id message)))
  [:div.tablestyle :table :tr.row :td.sender :a.username] (enlive/html-content (:username (find-user-by-id (:sender_id message))))
  [:div.tablestyle :table :tr.row :td.content :div.quote] (if(nil? (:quote message))
                                                             (enlive/set-attr :style "display:none")
                                                             (enlive/content (:quote message)))
  [:div.tablestyle :table :tr.row :td.content :p.post] (enlive/content (:content message))
  [:div.tablestyle :table :tr.row :td.content :p.datetime] (enlive/html-content 
                (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") (:created message)))
  [:td.content :p.quote :a.q] (enlive/set-attr :href (str "/messagereply/" (:id message))))

(enlive/deftemplate inbox-page  "inbox.html" [messages]
  [:title] (enlive/content "Forum topics")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [message messages]
  [:td.sender :a.username] (enlive/set-attr :href (str "/profile/" (:sender_id message)))
  [:td.sender :a.username] (enlive/html-content (:username (find-user-by-id (:sender_id message))))
  [:td.title :a.t] (enlive/set-attr :href (str "/message/" (:id message)))
  [:td.title :a.t] (if-not (nil? (:quote message))
                      (enlive/content (str "Re:"(:title message)))
                      (enlive/content (:title message)))
  [:td.datetime] (enlive/html-content 
                (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") (:created message)))))

(enlive/deftemplate outbox-page  "outbox.html" [messages]
  [:title] (enlive/content "Forum topics")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [message messages]
  [:td.receiver :a.username] (enlive/set-attr :href (str "/profile/" (:users_id message)))
  [:td.receiver :a.username] (enlive/html-content (:username (find-user-by-id (:users_id message))))
  [:td.title :a.t] (enlive/set-attr :href (str "/message/" (:id message)))
  [:td.title :a.t] (if-not (nil? (:quote message))
                      (enlive/content (str "Re:"(:title message)))
                      (enlive/content (:title message)))
  [:td.datetime] (enlive/html-content 
                (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") (:created message)))))

(enlive/deftemplate searchresult-page  "searchresult.html" [posts]
  [:title] (enlive/content "Forum topics")
  [:div.tablestyle :table :tr.row] (enlive/clone-for [post posts]
                [:td.author :a.username] (enlive/set-attr :href (str "/profile/" (:users_id post)))
                [:td.author :a.username] (enlive/html-content (:username (find-user-by-id (:users_id post))))
                [:td.content :div.quote] (if(nil? (:quote post))
                                           (enlive/set-attr :style "display:none")
                                           (enlive/content (:quote post)))
                [:td.content :p.post] (enlive/content (:content post))
                [:td.content :p.datetime] (enlive/html-content 
                (.format (java.text.SimpleDateFormat. "yyyy-MM-dd HH:mm:ss") (:created post)))
                [:td.content :p.topic :a.t] (enlive/set-attr :href (str "/post/" (:topics_id post)))
                ))
                

(enlive/deftemplate login-page "login.html" [& msg]
  [:div#error] (if (nil? msg)
                  (enlive/set-attr :style "display:none")
                  (do
                    (enlive/remove-attr :style)
                    (enlive/content msg))))

(enlive/deftemplate admin-page "admin.html" [])
(enlive/deftemplate category-page "category.html" [])
(enlive/deftemplate registration-page "register.html" [])
(enlive/deftemplate thread-page "thread.html" [])
(enlive/deftemplate addpost-page "comment.html" [])
(enlive/deftemplate addnews-page "addnews.html" [])
(enlive/deftemplate sendmessage-page "sendmessage.html" [])
(enlive/deftemplate search-page "search.html" [])